<template>

  <div id="uploadPhoto" class="uploadPhoto">
    <!-- header -->
    <yd-navbar slot="navbar" fixed>
      <router-link to="/user/usermsg" slot="left">
        <yd-navbar-back-icon></yd-navbar-back-icon>
      </router-link>
      <span slot="center" style="font-size:18px">修改头像</span>
    </yd-navbar>

    <!-- content -->
    <div class="content-uploadPhoto" fixed>
      <yd-cell-group>
        <yd-cell-item>
          <span slot="left">上传头像</span>
        </yd-cell-item>
        <form enctype="multipart/form-data" style="padding:15px 0;">
          <input style="padding-left:15px;width:100%;" type="file" ref="pic" name="file" />
        </form>
      </yd-cell-group>
      <yd-button size="large" @click.native="upload" bgcolor="#2196f3" color="#fff">上传</yd-button>
      <br>
      <!--
      <p>{{count}}</p>
      <button @click="increment">+1</button>
      <button @click="decrement">-1</button>
      -->
    </div>
  </div>
</template>

<script>
  import axios from '../../axios'
  import baseService from '../../axios/'
  export default {
    name: 'uploadPhoto',
    data() {
      return {
        message: '上传头像',
        file: '',
        photo: ''
      }
    },
    computed: {
      count() {
        return this.$store.store.state.count
      }
    },
    methods: {
      increment() {
        this.$store.store.commit('increment')
        // this.$store.store.state.count = 'sdasdas'
        // console.log(this.$store)
      },
      decrement() {
        this.$store.store.commit('decrement')
      },
      upload: function () {
        let file = this.$refs.pic.files[0];
        // console.log(file);
        let formData = new FormData()
        formData.append('file', file)
        baseService.post('/manage/product/upload.do', formData).then((res) => {
          if (res.data.status == 10 || res.data.status == 1) {
            this.$dialog.alert({
              mes: res.data.msg
            });
            this.$router.push({
              path: '/user/login'
            })
          }
          if (res.data.status == 0) {
            this.$dialog.alert({
              mes: "上传成功"
            })
            console.log(res.data.data.url);
            this.photo = res.data.data.url;
            console.log(this.photo);
            this.setData();
            // this.$router.push({
            //   path: '/user/usermsg'
            // })
            
          }
        })
      },
      setData: function () {
        let param = new URLSearchParams();   
        console.log(this.photo);   
        let photo = this.photo; 
        console.log(photo);
        param.append("photo", photo);
        baseService.post('/user/updateInformation.do', param).then((res) => {
          if (res.data.status == 0) {
            console.log(res.data)
          }
        })
      }
    }
  }

</script>

<style>
  .uploadPhoto .content-uploadPhoto {
    position: fixed;
    padding-top: 15%;
    height: 100%;
    width: 100%;
    overflow-y: auto !important;
  }

</style>
